/**
 * HelloPacket.java
 *
 * @author Hagen Boehm
 */


package SSF.OS.OSPFv2;


import java.util.*;
import SSF.OS.*;


// ========================= class SSF.OS.OSPFv2.HelloPacket ============================ //
/**
 * This class implements an OSPF Hello packet. The following from RFC 2328 was
 * not included:
 * 1.) Version
 * 2.) Packet length
 * 3.) Checksum
 * 4.) Authentication
 */
public class HelloPacket extends ProtocolMessage {

  public int netMask;
  public int helloInterval;
  public boolean[] options;
  public int routerPrio;
  public int routerDeadInterval;
  public int DR;
  public int BDR;
  // The router IDs of each router from whom valid Hello Packets have been seen.
  public Vector neighborIDs;
  // The standard length of a Hello Packet (without header!).
  public static final int STD_LENGTH = 20;

  // ----------------------------- constructors HelloPacket ---------------------------- //
  /** This one creates a Hello Packet for broadcast mediums */

  public HelloPacket(int netMask, int helloInterval, boolean[] options, int routerPrio, 
                     int routerDeadInterval, int DR, int BDR, Vector neighborIDs) {
    this.netMask            = netMask;
    this.helloInterval      = helloInterval;
    this.options            = (boolean[])options.clone();
    this.routerPrio         = routerPrio;
    this.routerDeadInterval = routerDeadInterval;
    this.DR                 = DR;
    this.BDR                = BDR;
    this.neighborIDs        = neighborIDs;
  }

  /** This one creates a Hello Packet for nonbroadcast mediums */

  public HelloPacket(int helloInterval, boolean[] options, int routerDeadInterval, Vector neighborIDs) {
    this.netMask            = 0;
    this.helloInterval      = helloInterval;
    this.options            = (boolean[])options.clone();
    this.routerPrio         = 0;
    this.routerDeadInterval = routerDeadInterval;
    this.DR                 = 0;
    this.BDR                = 0;
    this.neighborIDs        = neighborIDs;
  }

  // ----------------------------- HelloPacket.carryPayload ---------------------------- //

  public void carryPayload(ProtocolMessage payload) {
    System.err.println("** Warning: HelloPacket can't take payload; dropping.");
  }

  // ------------------------------- HelloPacket.bytecount ---------------------------- //
  /** Return the total size (in bytes) of this Hello Packet body. 
   *  All and only fields specified in RFC 2328 are included in calculation!!!
   */
  public int bytecount() {
    return header_bytecount();
  }

  // ---------------------------- HelloPacket.header_bytecount ------------------------- //
  /** Return the size (in bytes) of this header only, don't include the payload, if any. 
   *  Hello Packets don't have payload.
   */
  public int header_bytecount() {
    if (neighborIDs == null) {
      return STD_LENGTH;
    } else {
      return (STD_LENGTH + 4*neighborIDs.size());
    }
  }

  // ----------------------------- HelloPacket.toString -------------------------- //
  public String toString() {
    String s = "";
    s = netMask + " " + helloInterval + " ";
    for (int i=0; i<8; i++) {
      s+= (new Boolean(options[i])).toString()+ " ";
    }
    s+= routerPrio + " " + routerDeadInterval + " " + DR + " " + BDR;
    for (Enumeration e = neighborIDs.elements() ; e.hasMoreElements() ;) {
      s += " " + e.nextElement().toString();
    }  
    return s;
  }

} //end class HelloPacket







